package it.surface;


/*
 * Copyright (C) 2007 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


import java.util.ArrayList;
import java.util.Iterator;

import name.bagi.levente.pedometer.StepService;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.graphics.PointF;
import android.util.Log;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.view.View;
import android.view.View.OnTouchListener;

public class GraphicView extends View implements OnTouchListener {
	final static String THIS_FILE = "GraphicsView";
        private Paint   textPaint = new Paint();
        private Paint   yellowAlpha = new Paint();
        private Paint   heatPaint = new Paint();
        private Path    mPath = new Path();
        private Path    hPath = new Path();
        private boolean mAnimate;
        StepService servizietto;
        private int yTrim = 0;
        int screenox,screenoy;
        public  GraphicView(Context context,StepService service) {
        	super(context);
        	servizietto = service;
            //service.mStepDetector.isShake();
            // Construct a wedge-shaped path
            mPath.moveTo(0, -50);
            mPath.lineTo(-20, 60);
            mPath.lineTo(0, 50);
            mPath.lineTo(20, 60);
            mPath.close();
            this.setOnTouchListener(this);
            yellowAlpha.setARGB(0xc0, 0x0, 0x70, 0xc0);
            textPaint.setTextSize(28);
            textPaint.setTextScaleX(0.5f);
            movements.add(new PointF(0,0));
            
        }
        ArrayList movements = new ArrayList();
        static int count = 0;
        double _pi = 2 * Math.PI / 360.0;
        PointF prevoStep = new PointF(0,0);
        public void interrupt(){
        	count++;
        	//getWifiData();
        	PointF p = new PointF (0,0 );
        	p.x = prevoStep.x +  20 * (float)Math.cos(_pi * alpha );
        	p.y = prevoStep.y +  20 * (float)Math.sin(_pi * alpha );
        	movements.add(p);
        }
        public void heatRect( Canvas canvas, float x , float y , int red){
        	hPath = new Path();
        	hPath.moveTo(x - .7f * mPosX , y);
        	hPath.lineTo(x - .7f * mPosX, y + 1f * mPosX );
        	hPath.lineTo(x + .7f * mPosX , y + 1f * mPosX );
        	hPath.lineTo(x + .7f * mPosX , y);
        	hPath.close();
        	heatPaint.setARGB(0xEE, 255,120,120);
        	canvas.drawPath(hPath , heatPaint);
        }
        
        private void drawMapXZ(Canvas canvas){
        	Iterator it = movements.iterator();
            PointF prev = (PointF)it.next();
            while ( it.hasNext() ) {
                PointF p = (PointF)it.next();
                canvas.drawLine(prev.x , prev.y,  p .x , p.y, textPaint);
                prev = p;
            }
            
        	Grid grid = new Grid();
        	Point point = new Point();
            ArrayList alivePoint = new ArrayList();
         for ( int i = 0; i< grid.getSize() ; i ++)
             for ( int k = 0 ; k < grid.getSize() ;k ++ )
             
                 if (grid.getValue(i, k)!= 0)
                     alivePoint.add(new Point(i - 6 ,k - 6));
             it = alivePoint.iterator();
             while (it.hasNext()) {
                 Point cur = (Point)it.next();

                 heatRect(canvas, cur.x * 2.0f * mPosX  ,cur.y * 1.5f * mPosX,128);
             }
            
        }

        @Override protected void onDraw(Canvas canvas) {
        	//Log.e(THIS_FILE, "************ ondraw graphics again");
            

            canvas.drawColor(Color.WHITE);

            //textPaint.setAntiAlias(true);
            textPaint.setColor(Color.BLACK);
            //paint.setStyle(Paint.Style.FILL);

            int w = canvas.getWidth();
            int h = canvas.getHeight();
            screenox = w / 2;
            screenoy = h / 2;
            String text = String.format("Bello 6 z Value: %1.2f", servizietto.mStepDetector.getShake());
            	
            canvas.drawText( text  + " ), shake " + servizietto.mStepDetector.isShake() + ", count:" + count, 50, 60, textPaint);
            
            canvas.drawRect(10, 10, mPosX, mPosY , yellowAlpha); // TRIM
            canvas.translate(screenox, screenoy);
            drawMapXZ(canvas);
            //if (mValues != null) {
            alpha -= 1.0f;
                canvas.rotate(alpha);
            //}
            canvas.drawPath(mPath, textPaint);
            
        }

        @Override
        protected void onAttachedToWindow() {
            mAnimate = true;
            if (true) Log.d(THIS_FILE, "onAttachedToWindow. mAnimate=" + mAnimate);
            super.onAttachedToWindow();
        }

        @Override
        protected void onDetachedFromWindow() {
            mAnimate = false;
            if (true) Log.d(THIS_FILE, "onDetachedFromWindow. mAnimate=" + mAnimate);
            super.onDetachedFromWindow();
        }
        public void setStepService(StepService s){
            this.servizietto = s;
        }
        float alpha = 0;
        private float mPosX;
        private float mPosY;

        private float mLastTouchX;
        private float mLastTouchY;
        final int INVALID_POINTER_ID = -1;
        private int mActivePointerId = INVALID_POINTER_ID;

        private ScaleGestureDetector mScaleDetector;

		@Override
		public boolean onTouch(View v, MotionEvent ev) {

	        final int action = ev.getAction();
	        switch (action & MotionEvent.ACTION_MASK) {
	        case MotionEvent.ACTION_DOWN: {
	            final float x = ev.getX();
	            final float y = ev.getY();

	            mLastTouchX = x;
	            mLastTouchY = y;
	            //movements.add(new Point(Math.round(x) - screenox , Math.round(y) - screenoy));
	            
	            mActivePointerId = ev.getPointerId(0);
	            break;
	        }

	        case MotionEvent.ACTION_MOVE: {
	            final int pointerIndex = ev.findPointerIndex(mActivePointerId);
	            final float x = ev.getX(pointerIndex);
	            final float y = ev.getY(pointerIndex);

	            // Only move if the ScaleGestureDetector isn't processing a gesture.
	            //if (!mScaleDetector.isInProgress()) {
	                final float dx = x - mLastTouchX;
	                final float dy = y - mLastTouchY;

	                mPosX += dx;
	                mPosY += dy;

	                invalidate();
	            //}

	            mLastTouchX = x;
	            mLastTouchY = y;

	            break;
	        }

	        case MotionEvent.ACTION_UP: {
	            mActivePointerId = INVALID_POINTER_ID;
	            break;
	        }

	        case MotionEvent.ACTION_CANCEL: {
	            mActivePointerId = INVALID_POINTER_ID;
	            break;
	        }

	        case MotionEvent.ACTION_POINTER_UP: {
	            final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) 
	                    >> MotionEvent.ACTION_POINTER_INDEX_SHIFT;
	            final int pointerId = ev.getPointerId(pointerIndex);
	            if (pointerId == mActivePointerId) {
	                // This was our active pointer going up. Choose a new
	                // active pointer and adjust accordingly.
	                final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
	                mLastTouchX = ev.getX(newPointerIndex);
	                mLastTouchY = ev.getY(newPointerIndex);
	                mActivePointerId = ev.getPointerId(newPointerIndex);
	            }
	            break;
	        }
	        }

	        return true;
		}

		
    }

